<template>
	<body-wrap>
		<view class="topSearch">
			<my-search v-model="kwd" @confirm='doSearch'/>
		</view>
		<view class="comment">
			<view class="cmList" @click="goDetail(`/pages/qa/detail?id=${dt._id}`)" v-for="dt in lists" :key="dt._id">
				<view class="imgbox">
					<cm-image v-if="dt.userId" :src="dt.userId.avatar"/>
				</view>
				<view class="mainbox">
					<text class="name">{{dt.userId.name || dt.userId.nickname}}</text>
					<text class="time">{{dateFormat('Y-m-d H:i',dt.createTime)}}</text>
					<view class="cmDtBox">{{dt.questionContent}}</view>
					<view class="opbox">
						<view v-if="userInfo.type==1" @click.stop="goDetail(`/pages/qa/replyForm?id=${dt._id}`)" class="zjhfbtn">立即回复</view>
						<div class="numbox">
							<i class="iconfont icon-chakan">{{dt.reads}}</i>
							<i class="iconfont icon-31pinglun">{{dt.comments}}</i>
							<i class="iconfont icon-shoucang" @click.stop="add_Collect(dt)">{{dt.collects}}</i>
						</div>
					</view>
				</view>
			</view>
		</view>
		<empty v-if="lists && lists.length==0"/>
		<navigator url="/pages/question/add" class="wdbtn">开始咨询</navigator>
	</body-wrap>
</template>

<script>
	import {defineComponent,reactive,toRefs,onMounted,getCurrentInstance,computed} from 'vue'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import bodyWrap from "@/components/bodyWrap/index.vue"
	import cmImage from "@/components/cmImage/index.vue"
	import mySearch from "@/components/mySearch/index.vue"
	import empty from "@/components/empty/index.vue"
	import {dateFormat,toast} from "@/utils/common.js"
	import {getQuestionList,addCollect,getCollectList} from "@/api/consult.js"
	export default defineComponent({
		components:{
			bodyWrap,
			cmImage,
			mySearch,
			empty
		},
		props:{
		
		},
		setup(props,context) {
			const store = useStore()
			const {proxy} = getCurrentInstance()
			
			const state = reactive({
				userInfo:computed(()=>store.state.base.userInfo),
				lists:'',
				pageIndex:1,
				pageSize:20,
				kwd:''
			})
			
			onLoad(options=>{
				if(options && options.keyword){
					state.kwd = options.keyword
				}
				getQaList()
			})
			
			onMounted(()=>{
				
			})
			
			const getQaList = async ()=>{
				const res = await getQuestionList({
					privacy:1, // 公开
					pageIndex:state.pageIndex,
					pageSize:state.pageSize,
					from:'EJConsult',
					keywords:state.kwd
				})
				state.lists = res.data.data || []
			}
			
			const doSearch = ()=>{
				state.pageIndex = 1
				state.lists = []
				getQaList()
			} 
			
			const add_Collect = async (item)=>{
				const params = {
					projectId:item._id,
					userId:state.userInfo._id
				}
				const res = await getCollectList(params)
				if(res.code == 200 && res.data.data.length>0){
					return toast('已经收藏了')
				}
				
				const res2 = await addCollect({
					projectId:item._id,
					userId:state.userInfo._id
				})
				if(res2.code == 200){
					toast('收藏成功')
					item.collects ++
				}else{
					toast('收藏成失败')
				}
			}
			
			const goDetail = (url)=>{
				uni.navigateTo({url})
			}
			
			return {
				...toRefs(state),
				doSearch,
				dateFormat,
				add_Collect,
				goDetail
			}
		}
	})
</script>

<style lang="scss" scoped>
	.topSearch{
		padding: 20rpx;
	}
	.comment{
		padding: 0;
		box-sizing: border-box;
		width: 100%;
		.cmList{
			display: flex;
			box-sizing: border-box;
			padding: 20rpx;
			background: #fff;
			&+.cmList{
				border-top: 1px solid #f2f2f2;
			}
			.imgbox{
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				overflow: hidden;
				flex-shrink: 0;
			}
			.mainbox{
				flex: 1;
				box-sizing: border-box;
				flex-shrink: 0;
				padding: 0 0 0 10rpx;
				line-height: normal;
				width: 100%;
				.name{
					display: block;
					font-size: 32rpx;
					color: #e3af2f;
				}
				.time{
					display: block;
					font-size: 28rpx;
					color: #999;
					padding: 15rpx 0;
				}
				.cmDtBox{
					font-size: 28rpx;
					line-height: 46rpx;
				}
				.zjhf{
					background: #f7f6f2;
					padding: 15rpx;
					border-radius: 10rpx;
					margin: 10rpx 0 0 0;
					.title{
						display: block;
						font-size: 30rpx;
						font-weight: bold;
						color: #b47a48;
						padding: 0 0 10rpx 0;
					}
					.replayCon{
						font-size: 28rpx;
						line-height: 46rpx;
						color: #b47a48;
					}
				}
				.opbox{
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding: 15rpx 0 0 0;
					.zjhfbtn{
						display: flex;
						align-items: center;
						justify-content: center;
						height: 50rpx;
						padding: 0 20rpx;
						border-radius: 40rpx;
						background: #e3af2f;
						color: #fff;
						font-size: 26rpx;
					}
					.numbox{
						display: flex;
						flex: 1;
						align-items: center;
						justify-content: flex-end;
						.iconfont{
							font-size: 26rpx;
							color: #999;
							&+.iconfont{
								margin-left: 20rpx;
							}
							&:before{
								font-size: 30rpx;
								color: #333;
								font-weight: 700;
							}
						}
					}
				}
			}
		}
	}
	.wdbtn{
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		z-index: 1000;
		left: 50%;
		bottom: 40rpx;
		margin: 0 0 0 -100rpx;
		height: 80rpx;
		width: 200rpx;
		background: #d50000;
		font-size: 28rpx;
		color: #fff;
		border-radius: 10rpx;
	}
</style>
